/*
 *  Copyright (C) 2010 Glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.nbpam.windows;

import java.awt.Component;
import java.awt.Window;
import java.util.logging.Level;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import org.balisunrise.annotation.ActionCancel;
import org.balisunrise.annotation.ActionConfirm;
import org.balisunrise.application.Application;
import org.balisunrise.builder.BuilderException;
import org.balisunrise.common.VisualAction;
import org.balisunrise.common.EntityActions;
import org.balisunrise.common.PropertiesReader;
import org.balisunrise.common.Search;
import org.balisunrise.common.VisualEntity;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;

/**
 *
 * @author Glauco
 */
public class SearchDialog extends JDialog implements DialogView{

    private Search<?, ?, ?, JPanel> search;

    private boolean confirmed = false;

    private PropertiesReader pr;

    public SearchDialog(Window owner, Search search) {
        super(owner, search.getTitle(), ModalityType.APPLICATION_MODAL);
        this.setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
        this.search = search;
        pr = Application.get().getPropertiesReader();
        init();
    }

    private void init() {
        try{
            VisualAction confirmationAction = Application.get()
                    .getVisualEntityManager()
                    .getActionBuilder()
                    .createConfirmationAction(this, "");

            VisualAction cancellationAction = Application.get()
                    .getVisualEntityManager()
                    .getActionBuilder()
                    .createCancellationAction(this, "");

            EntityActions ea = search.getEntityActions();
            confirmationAction.setOwner(ea);
            cancellationAction.setOwner(ea);
            ea.setConfirmationAction(confirmationAction);
            ea.setCancellationAction(cancellationAction);

        }catch(BuilderException bex){
            Application.get().getLogger().log(Level.SEVERE,
                    "Falha ao inicial SearchDialog: " + bex.getMessage(), bex);
        }

        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        search.newSearch();
        this.getContentPane().add(search.getVisualRepresentation());

        this.pack();
    }

    public boolean isConfirmed() {
        return confirmed;
    }

    public void setConfirmed(boolean confirmed) {
        this.confirmed = confirmed;
    }

    @Override
    public void setVisible(boolean b) {
        if(b)
            confirmed = false;
        setLocationRelativeTo(
                (Component) Application.get().getVisualRepresentation());
        super.setVisible(b);
    }

    @ActionConfirm(alias="",
        text="PROP:org.balisunrise.text.select",
        toolTipText="PROP:org.balisunrise.tooltiptext.select")
    public void confirm(){

        if(search.getSelectedEntity() == null){
            NotifyDescriptor d = new NotifyDescriptor.Message(
                    pr.readProperty(pr.noSelectedEntity),
                    NotifyDescriptor.INFORMATION_MESSAGE);
            DialogDisplayer.getDefault().notify(d);
            return ;
        }

        confirmed = true;
        setVisible(false);
    }

    @ActionCancel(alias="",
        text="PROP:org.balisunrise.text.cancel",
        toolTipText="PROP:org.balisunrise.tooltiptext.cancel")
    public void cancel(){
        setVisible(false);
    }

    @Override
    public VisualEntity getVisualEntity() {
        return search;
    }

    @Override
    public Boolean getConfirmation() {
        return isConfirmed();
    }

    @Override
    public JDialog getVisualRepresentation() {
        return this;
    }
}
